#include "6502.hh"

namespace CPU
{
    CPU::CPU()
        : ram_(RAM_SIZE)
    {
    }

    CPU::~CPU()
    {
    }
    
    void CPU::reset()
    {
	pc_ = address_at(0xFFFC);
    }

    unsigned short CPU::address_at(const unsigned short address) const
    {
	return ram_.at(address+1) << 8 | ram_.at(address);
    }

    bool CPU::flag(const flags f) const
    {
        return p_ >> f & 1;
    }

    void CPU::load_ram(std::string filename)
    {
	/* Load up the file into rgb */
	std::ifstream ifs(filename.c_str());

	char* rgb = new char[RAM_SIZE];
	
	ifs.read(rgb, RAM_SIZE);
	ifs.close();

	/* Copy rgb into RAM */
	char* pch = rgb;
	for(std::vector<unsigned char>::iterator it = ram_.begin();
	    it != ram_.end(); it++, pch++)
	{
	    *it = *pch;
	}

	delete[] rgb;	
    }
}
